package com.jucai.pppeiyouclient.view.fragment;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.HistoricalActivityAdapter;
import com.jucai.pppeiyouclient.base.BaseFragment;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.ActivityBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.view.activity.CommonH5Activity;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Kuky on 2017-04-22.
 */

public class HistoricalActivitiesFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "HistoricalActivitiesFragment : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView historicalActivitiesRv;
    private List<ActivityBean> activityList = new ArrayList<>();
    private HistoricalActivityAdapter activityAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView() {
        activityAdapter = new HistoricalActivityAdapter(getActivity(), activityList);
        refreshLayout = (PullToRefreshLayout) itemView.findViewById(R.id.refresh_view);
        historicalActivitiesRv = (PullToRefreshForRecyclerView) itemView.findViewById(R.id.content_view);
        historicalActivitiesRv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        historicalActivitiesRv.setItemAnimator(new DefaultItemAnimator());
        historicalActivitiesRv.setAdapter(activityAdapter);
        activityAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                ActivityBean a = activityList.get(position);
                if (!TextUtils.isEmpty(a.getURL())) {
                    CommonH5Activity.startCommH5(getActivity(), 0, a.getURL(), a.getTactivity_id(),
                            null, a.getTactivity_title());
                    LogUtils.e(TAG + "URL = " + a.getURL());
                }
            }

            @Override
            public void onItemLongClickListener(View view, int position) {

            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("page", "1");
        param.put("status", "1");
        param.put("service", "Conductor_Activity.getlistuid");

        CommonRequestNetData.getJsonObjectData(getActivity(), param,true, new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) {
                LogUtils.e(TAG + "jsObj = " + jsonObject);
                try {
                    JSONArray array = jsonObject.getJSONArray("history");
                    List<ActivityBean> list = new Gson().fromJson(array.toString(), new TypeToken<List<ActivityBean>>() {
                    }.getType());
                    if (page == 1){
                        activityList.clear();
                    }
                    if (list != null && !list.isEmpty()) {
                        activityList.addAll(list);
                    } else
                       // ToastUtils.showToast("没有更多数据~");
                    activityAdapter.setData(activityList);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected int getFragmentViewId() {
        return R.layout.common_fragment_with_ptr;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> param = new HashMap<>();
                param.put("page", page + "");
                param.put("status", "1");
                param.put("service", "Conductor_Activity.getlistuid");

                CommonRequestNetData.getJsonObjectData(getActivity(), param, true,new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        LogUtils.e(TAG + "jsObj = " + jsonObject);
                        try {
                            JSONArray array = jsonObject.getJSONArray("history");
                            List<ActivityBean> list = new Gson().fromJson(array.toString(), new TypeToken<List<ActivityBean>>() {
                            }.getType());
                            if (list != null && !list.isEmpty()) {
                                activityList.addAll(list);
                                activityAdapter.setData(activityList);
                            } else
                                ToastUtils.showToast("没有更多数据~");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
